package com.lzx.applib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.lzx.applib.R;

/**
 * Created by Lzx on 16/12/06 0006.
 */
public class TriangleView extends View {

    public static final String TAG = "TriangleView";

    int solidColor;
    int solidSelectedColor;
    int angle;
    Paint solidPaint;
    Path path;

    public TriangleView(Context context) {
        super(context);
    }

    public TriangleView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TriangleView);
        solidColor = array.getColor(R.styleable.TriangleView_solidColor, Color.TRANSPARENT);
        solidSelectedColor = array.getColor(R.styleable.TriangleView_solidSelectedColor, solidColor);
        angle = array.getInt(R.styleable.TriangleView_angle, 0);
        array.recycle();
        setBackgroundColor(Color.TRANSPARENT);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (solidPaint == null) {
            initPaint();
            initPath();
        }

        canvas.drawPath(path, solidPaint);
        super.onDraw(canvas);
    }

    @Override
    public void setSelected(boolean selected) {
        if (selected) {
            solidPaint.setColor(solidSelectedColor);
        } else {
            solidPaint.setColor(solidColor);
        }
        super.setSelected(selected);
    }

    public void initPaint() {
        solidPaint = new Paint();
        solidPaint.setAntiAlias(true);
        solidPaint.setColor(solidColor);
        solidPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    public void initPath() {
        int width = getWidth();
        int height = getHeight();
        int[][] points = new int[][]{{0, 0}, {width, 0}, {width, height}, {0, height}};

        path = new Path();
        int start = angle/90;
        int[] p1 = points[(start+0)%4];
        path.moveTo(p1[0], p1[1]);

        int[] p2 = points[(start+1)%4];
        path.lineTo(p2[0], p2[1]);

        int[] p3 = points[(start+2)%4];
        path.lineTo(p3[0], p3[1]);
    }


}
